//
//  ForgetPwdViewController.m
//  Haliaeetus
//
//  Created by junli hou on 2018/5/2.
//  Copyright © 2018年 BMY. All rights reserved.
//

#import "ForgetPwdViewController.h"

@interface ForgetPwdViewController ()
@property (nonatomic, strong) UIButton *smsCodeBtn;
@property (nonatomic, strong) UITextField *phoneTextField;
@property (nonatomic, strong) UITextField *codeTextField;
//@property (nonatomic, strong) UITextField *nickNameField;
@property (nonatomic, strong) UITextField *pswField;
@property (nonatomic, strong) UIButton *signUpBtn;
@end

@implementation ForgetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"忘记密码";
    [self.navigationController setLeftBtnWithTarget:self action:@selector(goBack)];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self initView];
}
- (void)goBack{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)initView{
    
    //手机号码
    
    UILabel *phoneLab = [[UILabel alloc]initWithFrame:CGRectMake(12,64,90,59)];
    phoneLab.text = @"手机号";
    phoneLab.textColor = WS_RGBA(204,204,204,1);
    phoneLab.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:phoneLab];
    
    self.phoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(phoneLab.frame) + 12,64,self.view.frame.size.width - 120,59)];
    self.phoneTextField.keyboardType= UIKeyboardTypeNumberPad;
    self.phoneTextField.backgroundColor = [UIColor whiteColor];
    self.phoneTextField.placeholder=@"请填写您的手机号码";
    self.phoneTextField.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:self.phoneTextField];
    
    UIImageView *lineView = [[UIImageView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(phoneLab.frame),self.view.frame.size.width,1)];
    lineView.backgroundColor = WS_RGBA(225, 225, 225, 0.5);
    [self.view addSubview:lineView];
    
    UILabel *codeLab = [[UILabel alloc]initWithFrame:CGRectMake(12,CGRectGetMaxY(lineView.frame),65,59)];
    codeLab.text = @"验证码";
    codeLab.textColor = WS_RGBA(204,204,204,1);
    codeLab.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:codeLab];
    
    //验证码
    self.codeTextField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(phoneLab.frame) + 12,CGRectGetMaxY(lineView.frame),self.view.frame.size.width - 120 - 130,58)];
    self.codeTextField.keyboardType= UIKeyboardTypeNumberPad;
    self.codeTextField.backgroundColor = [UIColor whiteColor];
    self.codeTextField.font = [UIFont systemFontOfSize:16];
    self.codeTextField.placeholder=@"请填写验证码";
    [self.view addSubview:self.codeTextField];
    
    UIImageView *lineView1 = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 131,CGRectGetMaxY(lineView.frame),1,59)];
    lineView1.backgroundColor = WS_RGBA(225, 225, 225, 0.5);
    [self.view addSubview:lineView1];
    
    //获取验证码
    self.smsCodeBtn= [UIButton buttonWithType:UIButtonTypeCustom];
    self.smsCodeBtn.frame = CGRectMake(self.view.frame.size.width - 130,CGRectGetMaxY(lineView.frame),130,59);
    [self.smsCodeBtn setTitleColor:WS_RGBA(107, 165, 248, 1) forState:UIControlStateNormal];
    [self.smsCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.smsCodeBtn addTarget:self action:@selector(VerificationCode:) forControlEvents:UIControlEventTouchUpInside];
    self.smsCodeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    self.smsCodeBtn.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.smsCodeBtn];
    
    UIImageView *lineView2 = [[UIImageView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(codeLab.frame),self.view.frame.size.width,1)];
    lineView2.backgroundColor = WS_RGBA(225, 225, 225, 0.5);
    [self.view addSubview:lineView2];
    
    
//    UILabel *nickNameLab = [[UILabel alloc]initWithFrame:CGRectMake(12,CGRectGetMaxY(self.smsCodeBtn.frame),90,59)];
//    nickNameLab.text = @"昵称";
//    nickNameLab.textColor = WS_RGBA(204,204,204,1);
//    nickNameLab.font = [UIFont systemFontOfSize:14];
//    [self.view addSubview:nickNameLab];
//
//    self.nickNameField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(nickNameLab.frame) + 12,CGRectGetMaxY(codeLab.frame)+0.5,self.view.frame.size.width - 120,58)];
//    self.nickNameField.keyboardType= UIKeyboardTypeDefault;
//    self.nickNameField.backgroundColor = [UIColor whiteColor];
//    self.nickNameField.font = [UIFont systemFontOfSize:16];
//    self.nickNameField.placeholder=@"请填写昵称";
//    [self.view addSubview:self.nickNameField];
//
//    UIImageView *lineView3 = [[UIImageView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(nickNameLab.frame),self.view.frame.size.width,1)];
//    lineView3.backgroundColor = WS_RGBA(225, 225, 225, 0.5);
//    [self.view addSubview:lineView3];
    
    UILabel *pswLab = [[UILabel alloc]initWithFrame:CGRectMake(12,CGRectGetMaxY(self.smsCodeBtn.frame),90,59)];
    pswLab.text = @"新密码";
    pswLab.textColor = WS_RGBA(204,204,204,1);
    pswLab.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:pswLab];
    
    self.pswField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(pswLab.frame) + 12,CGRectGetMaxY(self.smsCodeBtn.frame)+0.5,self.view.frame.size.width - 120,58)];
    self.pswField.keyboardType= UIKeyboardTypeDefault;
    self.pswField.backgroundColor = [UIColor whiteColor];
    self.pswField.font = [UIFont systemFontOfSize:16];
    self.pswField.placeholder=@"请设置新密码";
    [self.view addSubview:self.pswField];
    UIImageView *lineView4 = [[UIImageView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(pswLab.frame),self.view.frame.size.width,1)];
    lineView4.backgroundColor = WS_RGBA(225, 225, 225, 0.5);
    [self.view addSubview:lineView4];
    
    
    
    
    self.signUpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.signUpBtn.frame = CGRectMake(20,CGRectGetMaxY(self.pswField.frame) + 30,self.view.frame.size.width-40,40);
    //    _LoginBtn.frame = CGRectMake(20,self.view.frame.size.height - 90 - 40,self.view.frame.size.width-40,40);
    self.signUpBtn.userInteractionEnabled = YES;
    [self.signUpBtn setTitle:@"修改" forState:UIControlStateNormal];
    self.signUpBtn.titleLabel.font = [UIFont systemFontOfSize: 16.0];
    [self.signUpBtn setTitleColor:WS_RGB(255, 255, 255) forState:UIControlStateNormal];
    self.signUpBtn.layer.masksToBounds = YES;
    self.signUpBtn.layer.cornerRadius = 2;
    self.signUpBtn.backgroundColor= WS_RGB(255, 134, 35);
    
    [self.signUpBtn addTarget:self action:@selector(doSignUpBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.signUpBtn];
    
}
-(void)startTime {
    __block int timeout=60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);//GCD定时器
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.smsCodeBtn setTitle:@"重发验证码" forState:UIControlStateNormal];
                self.smsCodeBtn.userInteractionEnabled = YES;
                //                [identifierBtn.layer setBorderColor:[ RGBA(208, 67, 69, 1) CGColor]];//边框颜色
                [self.smsCodeBtn setTitleColor:WS_RGBA(107, 165, 248, 1) forState:UIControlStateNormal];
            });
        }else{
            NSString *strTime = [NSString stringWithFormat:@"%.2d", timeout];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                NSLog(@"____%@",strTime);
                [self.smsCodeBtn setTitle:[NSString stringWithFormat:@"%@s重新获取",strTime] forState:UIControlStateNormal];
                self.smsCodeBtn.userInteractionEnabled = NO;
                //                [identifierBtn.layer setBorderColor:[[UIColor grayColor] CGColor]];//边框颜色
                [self.smsCodeBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                
            });
            timeout--;
            
        }
    });
    dispatch_resume(_timer);
}

- (void)VerificationCode:(id)sender{
    
    
    if ([self.phoneTextField.text isEqualToString:@""] || self.phoneTextField.text.length == 0) {
        WSToast(@"请输入您的手机号码")
        
    }else{
        
        [self validateVcodeHttpRequest];
    }
}

- (void)validateVcodeHttpRequest{
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [params setValue:self.phoneTextField.text forKey:@"mobile"];
    [WSNetwork WSHttpsByType:@"POST" Url:[NSString stringWithFormat:@"%@%@",BMY_URL,@"/passwords/send_code"] params:params onCompletion:^(NSMutableDictionary *dic){
        if ([[dic objectForKey:@"biz_action"] intValue]==0) {
            //获取成功
            WSToast(@"验证码发送成功");
            [self startTime];
        }else{
            //错误提示
               WSToast([dic objectForKey:@"biz_msg"]);
        }
        
    }onError:^(NSError *error){
        WSToast(@"验证码发送失败，稍候重试")
    }];
    
}

- (void)doSignUpBtn{
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [params setValue:self.phoneTextField.text forKey:@"mobile"];
    [params setValue:self.codeTextField.text forKey:@"auth_code"];
//    [params setValue:self.nickNameField.text forKey:@"nickname"];
    [params setValue:self.pswField.text forKey:@"password"];
    [WSNetwork WSHttpsByType:@"POST" Url:[NSString stringWithFormat:@"%@%@",BMY_URL,@"/passwords/reset"] params:params onCompletion:^(NSMutableDictionary *dic){
        if ([[dic objectForKey:@"biz_action"] intValue]==0) {
            //获取成功
            WSToast(@"密码修改成功，请重新登录。");

            [self goBack];
        }else{
            //错误提示
            WSToast([dic objectForKey:@"biz_msg"]);
        }
        
    }onError:^(NSError *error){
        WSToast(@"密码修改失败")
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
